<?php
function page_nav() {
	$query = $GLOBALS['wp_query'];
	$posts_per_page = 10;
	$paged = max( 1, absint( $query->get( 'paged' ) ) );
	$total_pages = max( 1, absint( $query->max_num_pages ) );
	if ( 1 == $total_pages)
		return;

	$request = $query->request;
	$numposts = $query->found_posts;

	$pages_to_show = 10;
	$pages_to_show_minus_1 = $pages_to_show - 1;
	$half_page_start = floor( $pages_to_show_minus_1/2 );
	$half_page_end = ceil( $pages_to_show_minus_1/2 );
	$start_page = $paged - $half_page_start;
	
	if ( $start_page <= 0 )
		$start_page = 1;

	$end_page = $paged + $half_page_end;

	if ( ( $end_page - $start_page ) != $pages_to_show_minus_1 )
		$end_page = $start_page + $pages_to_show_minus_1;

	if ( $end_page > $total_pages ) {
		$start_page = $total_pages - $pages_to_show_minus_1;
		$end_page = $total_pages;
	}

	if ( $start_page < 1 )
		$start_page = 1;

	$out = '';

	if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
		$first_text = str_replace( '%TOTAL_PAGES%', $total_pages, 'newest');
		$out .= _wp_pagenavi_single( 1, 'first', $first_text, '%TOTAL_PAGES%' );
		$out .= "<span class='extend'>{'...'}</span>";
	}

	$out .= '<td><span class="pre">'.get_previous_posts_link( '上一页' ).'</span></td><td class="pagenumber">';

	foreach ( range( $start_page, $end_page ) as $i ) {
		$out .= "<a href='" . esc_url( get_pagenum_link( $i ) ) . "' ";
		if ( $i == $paged ) {
			$out .= 'class="current"';
		}
		$out .= ">$i</a>";
	}

	$out .= '</td><td><span class="next">'.get_next_posts_link( '下一页', $total_pages ).'</span></td>';

	if ( $end_page < $total_pages ) {
		$out .= "<span class='extend'>{'...'}</span>";

		$out .= _wp_pagenavi_single( $total_pages, 'last', '最旧', '%TOTAL_PAGES%' );
	}

	echo $out;
}